package cn.ecnu.controller;


import cn.ecnu.constant.ResponseStatus;
import cn.ecnu.entity.Address;
import cn.ecnu.entity.Car;
import cn.ecnu.feign.UserFeignService;
import cn.ecnu.service.AddressService;
import cn.ecnu.utils.R;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/address")
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private UserFeignService userFeignService;

    @GetMapping("/users")
    public R getFeignUser() {
        return userFeignService.getUserList();
    }


    @GetMapping("/list")
    public R getCarList() {
        List<Address> result = addressService.list();
        return R.ok().put("data", result);
    }

    @PostMapping("/insert")
    public R insertAddress(@RequestBody Address address) {
        boolean result = addressService.save(address);
        if (result) return R.ok().put("data", address);
        return R.error(ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }

    @PostMapping("/update")
    public R updateAddress(@RequestBody Address address) {
        System.out.println(address);
        boolean result = addressService.updateById(address);
        if (result) return R.ok().put("data", address);
        return R.error(ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }

    @GetMapping("/{id}")
    public R getAddressById(@PathVariable("id") Integer id) {
        Address address = addressService.getById(id);
        return R.ok().put("data", address);
    }

    @GetMapping("/user/{uid}")
    public R getAddressByUid(@PathVariable("uid") String uid) {
        List<Address> adds = addressService.list(new QueryWrapper<Address>().eq("uid", uid));
        return R.ok().put("data", adds);
    }



    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        boolean result = addressService.removeById(id);
        if (result) return R.ok();
        return R.error(ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }




}
